package id.agung.wonogirimaps;

import java.io.File;

import com.actionbarsherlock.app.SherlockActivity;
import com.google.android.gms.maps.model.LatLng;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class InfoLocationActivity extends SherlockActivity implements
		OnClickListener
{
	private TextView					txtDesc;
	private ImageView					imgLocation;
	private Button						btnMap;

	private LatLng						location;

	private ImageLoader					imageLoader;
	private ImageLoaderConfiguration	config;
	private DisplayImageOptions			options;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_info_location);

		txtDesc = (TextView) findViewById(R.id.content_description);
		imgLocation = (ImageView) findViewById(R.id.image_single);
		btnMap = (Button) findViewById(R.id.btnMap);

		getSupportActionBar().setIcon(android.R.color.transparent);

		btnMap.setOnClickListener(this);

		// setting image loader

		File cacheDir = StorageUtils.getCacheDirectory(this);

		imageLoader = ImageLoader.getInstance();

		/**
		 * options gambar untuk imageLoader - menentukan gambar yang ditampilkan
		 * ketika gambar belom ke download
		 * 
		 * - set cache disc - set cache memory
		 */
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub).cacheOnDisc(true)
				.delayBeforeLoading(100).cacheInMemory(true).build();

		/**
		 * configurasi untuk imageloader
		 */
		config = new ImageLoaderConfiguration.Builder(this)
				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
				.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
				.writeDebugLogs().build();
		/**
		 * init imageloader dengan configurasinya
		 */
		imageLoader.init(config);

		/**
		 * Ambil data dari activity LocationActivity
		 */
		Bundle b = getIntent().getExtras();
		if (b != null)
		{
			getSupportActionBar().setTitle(
					b.getString(LocationActivity.TAG_TITLE));
			txtDesc.setText(b.getString(LocationActivity.TAG_DESC));
			location = new LatLng(b.getDouble(LocationActivity.TAG_LAT),
					b.getDouble(LocationActivity.TAG_LNG));
			imageLoader.displayImage(b.getString(LocationActivity.TAG_GAMBAR),
					imgLocation, options);
		}
	}

	@Override
	public void onClick(View v)
	{
		if (v == btnMap)
		{
			// String label = (String) getSupportActionBar().getTitle();
			// String uriBegin = "geo:" + location.latitude + ","
			// + location.longitude;
			// String query = location.latitude + "," + location.longitude + "("
			// + label + ")";
			// String encodedQuery = Uri.encode(query);
			// String uriString = uriBegin + "?q=" + encodedQuery + "&z=16";
			// Uri uri = Uri.parse(uriString);

			Intent i = new Intent(android.content.Intent.ACTION_VIEW,
					Uri.parse("http://maps.google.com/maps?daddr="
							+ location.latitude + "," + location.longitude));
			startActivity(i);
		}
	}

	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
	}
}
